﻿using AnesSystem.Models;
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AnesSystem.Controls
{
    public class RealVitalIcon : BaseIcon
    {

        #region Variables

        private bool _isChangeData = false;

        #endregion

        #region Constructor

        public RealVitalIcon()
        {

            this.MouseMove += RealVitalIcon_MouseMove;
            this.MouseLeave += RealVitalIcon_MouseLeave;
            this.DataContextChanged += RealVitalIcon_DataContextChanged;
            this.Loaded += RealVitalIcon_Loaded;
        }

        #endregion

        #region Properties

        public double CanvasLeft { get; set; }

        public double CanvasTop { get; set; }

        public PatientVitalSignsModel LeftVital { get; set; }

        public PatientVitalSignsModel RightVital { get; set; }

        public PatientVitalSignsModel SelfVital { get; set; }

        #endregion

        #region 依赖属性

        public Point LeftPoint
        {
            get { return (Point)GetValue(LeftPointProperty); }
            set { SetValue(LeftPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LeftPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LeftPointProperty =
            DependencyProperty.Register("LeftPoint", typeof(Point), typeof(BaseIcon), new PropertyMetadata(null));



        public Point RightPoint
        {
            get { return (Point)GetValue(RightPointProperty); }
            set { SetValue(RightPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RightPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RightPointProperty =
            DependencyProperty.Register("RightPoint", typeof(Point), typeof(BaseIcon), new PropertyMetadata(null));

        public double X1
        {
            get { return (double)GetValue(X1Property); }
            set { SetValue(X1Property, value); }
        }

        // Using a DependencyProperty as the backing store for X1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty X1Property =
            DependencyProperty.Register("X1", typeof(double), typeof(BaseIcon), new PropertyMetadata(1.0));



        public double X2
        {
            get { return (double)GetValue(X2Property); }
            set { SetValue(X2Property, value); }
        }

        // Using a DependencyProperty as the backing store for X2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty X2Property =
            DependencyProperty.Register("X2", typeof(double), typeof(BaseIcon), new PropertyMetadata(1.0));



        public double Y1
        {
            get { return (double)GetValue(Y1Property); }
            set { SetValue(Y1Property, value); }
        }

        // Using a DependencyProperty as the backing store for Y1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Y1Property =
            DependencyProperty.Register("Y1", typeof(double), typeof(BaseIcon), new PropertyMetadata(1.0));



        public double Y2
        {
            get { return (double)GetValue(Y2Property); }
            set { SetValue(Y2Property, value); }
        }



        // Using a DependencyProperty as the backing store for Y2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Y2Property =
            DependencyProperty.Register("Y2", typeof(double), typeof(BaseIcon), new PropertyMetadata(1.0));

        #endregion

        #region Event

        #endregion

        #region Private Methods


        #endregion

        #region Public/Protected Methods

        protected virtual int CalValue2Xplot(PatientVitalSignsModel iItem)
        {
            if (Constants.ISLABOR.Equals(DataManager.Instance.IsInAorP))
            {
                int index = iItem.LaborIndex;
                if (index <= 12)
                {
                    return(int) (index * LocationUtil.RealTimeItem_Width - Height / 2);
                }
                else
                {
                    return (int)(((index-12)+index)* LocationUtil.RealTimeItem_Width - Height / 2);
                }
            }
            else
            {
                DateTime startTime = new DateTime();
                if (iItem.IsAorP.Equals(Constants.ISANES))
                {
                    DateTime anesRecordTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
                    startTime = anesRecordTime.AddHours((CurPage - 1) * 4);
                }
                if (iItem.IsAorP.Equals(Constants.ISPACU))
                {
                    DateTime pacuRecordTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
                    startTime = pacuRecordTime.AddHours((CurPage - 1) * 4);
                }
                TimeSpan span = iItem.CaptureTime - startTime;
                double span1 = span.Minutes + span.Hours * 60 + span.Days * 24 * 60;
                double x = span1 / 5 * LocationUtil.RealTimeItem_Width - Height / 2;
                return Convert.ToInt32(x);
            }
        }

        protected virtual int CalValue2Yplot(PatientVitalSignsModel iItem)
        {
            if (string.IsNullOrEmpty(iItem.ModValue)) return 0;
            double value = Convert.ToDouble(iItem.ModValue);
            double lineHeight = LocationUtil.RealTimeSignItem_TotalRows * LocationUtil.RealTimeSignItem_Height;
            return CheckValue2Yplot(Convert.ToInt32(lineHeight - LocationUtil.RealTimeSignItem_Height * value / LocationUtil.SignSpan_Value - this.Height / 2));

        }

        public virtual double CalY2Valueplot(double top)
        {
            double lineHeight = LocationUtil.RealTimeSignItem_TotalRows * LocationUtil.RealTimeSignItem_Height;
            return CheckY2Valueplot(Convert.ToInt32(LocationUtil.SignEnd_Value - LocationUtil.SignEnd_Value / lineHeight * (top + this.Height / 2)));
        }
        public virtual double CheckY2Valueplot(double value)
        {
            if (value < 0)
            {
                return 0;
            }
            else if (value > 280)
            {
                return 280;
            }
            else
            {
                return value;
            }
        }
        public virtual int CheckValue2Yplot(int y)
        {
            if (y < 0)
            {
                return 0;
            }
            else if (y > LocationUtil.RealTimeSignItem_Height * LocationUtil.RealTimeSignItem_TotalRows)
            {
                return LocationUtil.RealTimeSignItem_Height * LocationUtil.RealTimeSignItem_TotalRows;
            }
            else
            {
                return y;
            }
        }

        public override void SetPoint(double left, double top)
        {
            try
            {
                Canvas.SetLeft(this, left);
                Canvas.SetTop(this, top);
                CanvasLeft = left;
                CanvasTop = top;
                CalculateXY();
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        public virtual void SetPoint(PatientVitalSignsModel value)
        {
            try
            {
                if (string.IsNullOrEmpty(value.ModValue))
                {
                    Visibility = Visibility.Collapsed;
                    //X1 = X1 - LocationUtil.RealTimeItem_Width;
                }
                else if (!string.IsNullOrEmpty(value.ModValue) && Visibility == Visibility.Collapsed)
                {
                    Visibility = Visibility.Visible;
                }
                double left = CalValue2Xplot(value);
                double top = CalValue2Yplot(value);
                Canvas.SetLeft(this, left);
                Canvas.SetTop(this, top);
                CanvasLeft = left;
                CanvasTop = top;
                CalculateXY();
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        public virtual void CalculateXY()
        {
            //X2 = CanvasLeft;
            //Y2 = CanvasTop + this.Height / 2;
            //X1 = CanvasLeft + this.Width;
            //Y1 = CanvasTop + this.Height / 2;
            //LeftPoint = new Point(X2, Y2);
            //RightPoint = new Point(X1, Y1);
            X1 = CanvasLeft + this.Width / 2;
            Y1 = CanvasTop + this.Height / 2;
            X2 = X1;
            Y2 = Y1;
            LeftPoint = new Point(X1, Y1);
            RightPoint = new Point(X2, Y2);


        }


        public void ChangeData(double value)
        {
            try
            {
                _isChangeData = true;
                SelfVital.ModValue = CalY2Valueplot(value).ToString();
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        public override void ResizeIcon()
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region EventHandlers

        private void RealVitalIcon_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (sender is TableCellCtrl) return;
            RenderTransform = new ScaleTransform(1.4, 1.4);
        }

        private void RealVitalIcon_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (sender is TableCellCtrl) return;
            RenderTransform = new ScaleTransform(1, 1);
        }

        private void RealVitalIcon_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void RealVitalIcon_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("ModValue"))
            {
                SetPoint(sender as PatientVitalSignsModel);
            }
        }

        private void RealVitalIcon_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext is PatientVitalSignsModel)
            {
                SelfVital = this.DataContext as PatientVitalSignsModel;
                SelfVital.PropertyChanged += RealVitalIcon_PropertyChanged;
            }
        }

        #endregion
    }
}
